package
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.clearTimeout;
	import flash.utils.setTimeout;
	import mylist.logs;
	
	/**
	 * ...
	 * @author db0@qq.com
	 */
	public class SocketItem
	{
		private var sock:Socket;
		private var photoDataId:int;
		private var curLength:uint = 0;
		private var isFile:Boolean = false;
		private var isHead:Boolean = false;
		
		public function SocketItem()
		{
		
		}
		
		public function init(sock:Socket):void
		{
			this.sock = sock;
			sock.addEventListener(Event.CLOSE, closed);
			sock.addEventListener(IOErrorEvent.IO_ERROR, errors);
			sock.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errors);
			sock.addEventListener(ProgressEvent.SOCKET_DATA, onClientSocketData);
			logs.adds("Connection from " + sock.remoteAddress + ":" + sock.remotePort);
			
			sendData();
		}
		
		private function errors(e:Event):void
		{
			clearTimeout(timeOutId);
			logs.adds(e);
		}
		
		private function closed(e:Event):void
		{
			clearTimeout(timeOutId);
			logs.adds(sock.remoteAddress + ":" + sock.remotePort + " closed");
		}
		
		/*
		   2:字符串 第一字节 长度(uint)
		   3:bytearr 第一字节 长度(uint)
		 */
		private function onClientSocketData(e:ProgressEvent):void
		{
			var buffer:ByteArray = new ByteArray();
			sock.readBytes(buffer);
			buffer.position = 0;
			curLength += buffer.length;
			if(isFile)isHead = false;//不含头 bmpd
			if(buffer.length >64){//bmpd 第一次
				if(isFile==false)isHead = true;
				isFile = true;
			}
			if (isHead)//
			{
				logs.adds("----------isHead-------", buffer.length);
				isFile = true;
				isHead = true;
				decodeCmd0(buffer);
			}
			else
			{
				if (isFile)
				{
					var photoData:PhotoData = PhotoData.photoDataList[photoDataId];
					if (photoData.fileSize > 0)
					{
						logs.adds("---not head----", buffer.length, buffer.position);
						photoData.appendData(buffer);
					}
				}
				else
				{
					decodeCmd0(buffer);
				}
				
			}
		}
		
		/**
		 *
		 * @param	buffer
		 */
		private function decodeCmd0(buffer:ByteArray):void
		{
			logs.adds("decodeCmd0()pos:", buffer.position, buffer.length);
			var cmd:uint = buffer.readUnsignedInt();
			switch (cmd)
			{
				case 1: 
					decodeCmd1(buffer);
					break;
				case 2: 
					decodeCmd2(buffer);
					break;
				case 3: 
					decodeCmd3(buffer);
					break;
				case 4: 
					decodeCmd4(buffer);
					break;
				default: 
			}
		}
		
		private function decodeCmd1(buffer:ByteArray):void
		{
			var cmd:uint = buffer.readUnsignedInt();
			
			logs.adds("uint:", cmd);
			if (buffer.length > buffer.position)
				decodeCmd0(buffer);
		}
		
		private function decodeCmd2(buffer:ByteArray):void
		{
			var len:uint = buffer.readUnsignedInt();
			logs.adds("str:", buffer.readUTFBytes(len));
			if (buffer.length > buffer.position)
				decodeCmd0(buffer);
		}
		
		/**
		 * bytes
		 * @param	buffer
		 */
		private function decodeCmd3(buffer:ByteArray):void
		{
			//var length:uint = buffer.readUnsignedInt();
			var photoData:PhotoData = PhotoData.photoDataList[photoDataId];
			if (photoData.fileSize > 0)
			{
				logs.adds(buffer.length, buffer.position);
					photoData.appendData(buffer);
			}
		}
		
		/**
		 * cmd
		 * @param	buffer
		 */
		private function decodeCmd4(buffer:ByteArray):void
		{
			var cmd:uint = buffer.readUnsignedInt();
			
			switch (cmd)
			{
				case 1:
					
					var fileSize:uint = (buffer.readUnsignedInt());
					var file_time:uint = (buffer.readUnsignedInt());
					var file_ip:uint = (buffer.readUnsignedInt());
					//var photoData:PhotoData = new PhotoData(fileSize, file_time, file_ip);
					//PhotoData.photoDataList.push(photoData);
					
					photoDataId = PhotoData.getPhotoIndex(fileSize, file_time, file_ip);
					logs.adds("creat:");
					logs.adds("id", photoDataId);
					logs.adds("size", fileSize, "time", file_time, "ip", file_ip);
					
					//logs.adds("photoDataId",photoDataId,ClientSocket.photoDataList[photoDataId].ip);
					break;
				case 2: 
					logs.adds("send  end");
					break;
				case 3: 
					var size:uint = buffer.readUnsignedInt();
					var time:uint = buffer.readUnsignedInt();
					var ip:uint = buffer.readUnsignedInt();
					var photoData:PhotoData = PhotoData.photoDataList[PhotoData.getPhotoIndex(size, time, ip)];
					logs.adds("VVVVVVVV", time, ip);
					if (photoData)
					{
						logs.adds("info:", photoData.fileSize);
						photoData.showData(size, time, ip);
					}
					break;
				case 4: //del
					logs.adds("XXXXXXXXXX");
					break;
				case 5: 
					logs.adds("err:", buffer.readUnsignedInt());
					decodeCmd0(buffer);
					break;
				case 6: 
					logs.adds("bmp");
					decodeCmd3(buffer);
					break;
				default: 
			}
			logs.adds("postion",buffer.position);
				if (buffer.length > buffer.position)
					decodeCmd0(buffer);
		}
		
		private var timeOutId:uint;
		
		private function sendData():void
		{
			clearTimeout(timeOutId);
			timeOutId = setTimeout(sendData, 20);
			if (curLength > 0 && sock.connected)
			{
				sock.writeUnsignedInt(curLength);
				logs.adds(sock.remoteAddress + ":" + sock.remotePort + ">>" + curLength);
				sock.flush();
			}
		}
	}
}
